package org.springblade.modules.policy.wrapper;

import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.policy.entity.LabelItem;
import org.springblade.modules.policy.service.ILabelItemService;
import org.springblade.modules.policy.vo.LabelItemVO;

import java.util.List;
import java.util.stream.Collectors;

public class LabelItemWrapper extends BaseEntityWrapper<LabelItem, LabelItemVO> {

	private static ILabelItemService labelItemService;

	static {
		labelItemService = SpringUtil.getBean(ILabelItemService.class);
	}

	public static LabelItemWrapper build() {
		return new LabelItemWrapper();
	}

	@Override
	public LabelItemVO entityVO(LabelItem LabelItem) {
		LabelItemVO LabelItemVO = BeanUtil.copy(LabelItem, LabelItemVO.class);
		if (Func.equals(LabelItem.getParentId(), CommonConstant.TOP_PARENT_ID)) {
			LabelItemVO.setParentName(CommonConstant.TOP_PARENT_NAME);
		} else {
			LabelItem parent = labelItemService.getById(LabelItem.getParentId());
			LabelItemVO.setParentName(parent.getLabel());
		}
		return LabelItemVO;
	}

	public List<INode> listNodeVO(List<LabelItem> list) {
		List<INode> collect = list.stream().map(Label -> BeanUtil.copy(Label, LabelItemVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}
}
